package seGobangClient.onlineHall;

import WebServiceLoginClient.*;
import seGobangClient.IUISystem;
import seGobangClient.Rule;

/**
 *
 * @author plus
 */
public class Server {
    private OnlineHallController MyController;
    private String URL=null;
    Room [] Rooms=null;
    Room currentRoom=null;
    private LoginClient login=new LoginClient();

    public Server(OnlineHallController MyController, String URL) {
        this.MyController = MyController;
        this.URL=URL;
    }
    
    /**
     * 测试服务器可用
     */
    public void testServer(){
         ((IUISystem)MyController.Scene).currentSceneChangeTo("OnlineHall");
         //if(WSTryConnect())
        if(login.TryConnect()){
           
            MyController.doEvent("LoginView", null);        
        }else{
            MyController.doEvent("ServerNotAvailable", null);        
        }
            
         //else
            
         
    }
    /**
     * 尝试登陆
     */
     public void Login(String UserName, String Password){
         //MyController.UID=WSLogin(UserName,Password);
         MyController.UserName=UserName;
         
         MyController.UID=login.login(Integer.toString(MyController.UID),UserName, Password);
         if (MyController.UID!=0){       //登陆成功
             MyController.doEvent("RoomListView", null);
         }else{                         //登陆失败
             MyController.doEvent("LoginFailed", null);
         }
     }
     /**
     * 读取房间列表
     */
    public void LoadRooms(){
        Rooms=new Room[]{new Room(MyController,1,"寝室","无禁手、不限时！~",new Rule(-1,0,false,false)),
                         new Room(MyController,2,"教学楼","无禁手、5分钟限时。",new Rule(300,0,false,false)),
                         new Room(MyController,3,"研究所","先手禁手、3分钟限时！",new Rule(180,0,false,false))};
        MyController.doEvent("RefreshRoomList", Rooms);
    }
    
    public void joinRoom(int id){
        this.currentRoom=Rooms[id];
        this.currentRoom.joinRoom();
    }
    
    public void exitRoom(){
        currentRoom.exitRoom();
    }
    
    public void joinDesk(int DeskID){
        currentRoom.joinDesk(DeskID);
    }
    
    public void exitDesk(){
        currentRoom.exitDesk();
    }
    
    public void getReady(){
        currentRoom.getReady();
    }
    
    public void cancelReady(){
        currentRoom.cancelReady();
    }
    
    public void pasueServer(){
        this.currentRoom.pauseRoom();
    }
    
    public void resumeServer(){
        this.currentRoom.resumeRoom();
        MyController.doEvent("DeskView", null);    
    }
}
